package com.ponhu.ea2.stockcenter.provider.service.entity.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.stockcenter.dto.WMSEventInfoDTO;
import com.ponhu.ea2.stockcenter.dto.WMSInventoryInfo;
import com.ponhu.ea2.stockcenter.enums.ExchangeActionTypeEnum;
import com.ponhu.ea2.stockcenter.enums.ResponseCodeEnum;
import com.ponhu.ea2.stockcenter.provider.entity.StockWarehouseChange;
import com.ponhu.ea2.stockcenter.provider.entity.StockWarehouseRelation;
import com.ponhu.ea2.stockcenter.provider.mapper.StockWarehouseChangeMapper;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockWarehouseChangeService;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockWarehouseChangeSnapshotService;
import com.ponhu.ea2.stockcenter.provider.support.exception.StockRuntimeException;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ponhu.ea2.stockcenter.enums.ResponseCodeEnum.STOCK_WAREHOUSE_EXCEPTION_900103;

/**
 * 仓库库存变动记录表服务实现类
 */
@Service
public class StockWarehouseChangeServiceImpl
        extends ServiceImpl<StockWarehouseChangeMapper, StockWarehouseChange>
        implements StockWarehouseChangeService {

    @Autowired
    private StockWarehouseChangeSnapshotService stockWarehouseChangeSnapshotService;


    /**
     * 批量保存仓库库存变动记录
     *
     * @param stockWarehouseRelations
     * @param eventInfoDTO
     * @return
     */
    @Override
    @Transactional
    public List<StockWarehouseChange> saveBatchChanges(List<StockWarehouseRelation> stockWarehouseRelations,
                                                       WMSEventInfoDTO eventInfoDTO) {

        Map<String, Long> ivtMap = stockWarehouseRelations.stream().collect(Collectors
                .toMap(StockWarehouseRelation::getInventoryCode, StockWarehouseRelation::getInventoryId));

        List<StockWarehouseChange> changeList = eventInfoDTO.getInventoryInfoList()
                .parallelStream().map(info -> new StockWarehouseChange()
                        .setInventoryId(ivtMap.get(info.getInventoryCode()))
                        .setInventoryCode(info.getInventoryCode())
                        .setInWarehouseCode(eventInfoDTO.getInWarehouseCode())
                        .setInWarehouseSale(eventInfoDTO.getInWarehouseIsSale())
                        .setOutWarehouseCode(eventInfoDTO.getOutWarehouseCode())
                        .setOutWarehouseSale(eventInfoDTO.getOutWarehouseIsSale())
                        .setTransferNo(eventInfoDTO.getTransferNo())
                        .setDirectSn(eventInfoDTO.getDocNo())
                        .setUniqueCode(info.getUniqueCode())
                        .setActionType(eventInfoDTO.getActionType().getCode())
                        .setUpdatedNum(info.getInventoryQuantity()) // 此处保持原正负值
                        .setOptRemark(eventInfoDTO.getActionType().getName()
                                + (eventInfoDTO.isFirstFrequency() ? "" : "[Redis缓存处理]")))
                .collect(Collectors.toList());

        if (BooleanUtils.isNotTrue(saveBatch(changeList)))
            throw new StockRuntimeException(STOCK_WAREHOUSE_EXCEPTION_900103);

        // 批量保存仓库库存变动快照
        stockWarehouseChangeSnapshotService
                .saveBatchSnapshots(changeList, stockWarehouseRelations);

        return changeList;
    }


    /**
     * 根据调拨单号和动作类型查询调拨记录
     *
     * @param transferNo
     * @param actionTypeEnums
     * @return
     */
    @Override
    public List<StockWarehouseChange> getStockWarehouseChanges(String transferNo,
                                                               List<ExchangeActionTypeEnum> actionTypeEnums) {

        List<Integer> actionTypes = actionTypeEnums
                .stream().map(ExchangeActionTypeEnum::getCode).collect(Collectors.toList());
        LambdaQueryWrapper<StockWarehouseChange> queryWrapper = Wrappers
                .lambdaQuery(StockWarehouseChange.class)
                .eq(StockWarehouseChange::getTransferNo, transferNo)
                .in(StockWarehouseChange::getActionType, actionTypes);

        return list(queryWrapper);
    }

}




